package com.base.cn.platform.os.controller.web.user.center.academic;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.manage.subject.SubjectService;
import com.base.cn.platform.os.service.manage.user.user.CusUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户学业信息
 * @author s.li
 * @create 2018-06-06-10:03
 */
@Controller
@RequestMapping("/web/uc/academic")
public class WebAcademicController extends WebBaseController {

    @Autowired
    private SubjectService subjectService;
    @Autowired
    private CusUserService cusUserService;

    /**
     * 我的学业
     * @param request
     * @return
     */
    @RequestMapping("/ajax/myAcademic")
    public String myAcademic(HttpServletRequest request,Model model){
        BigDecimal userId= webMobileLoginUtil.getLoginUserId(request);
        //查询用户学业信息
        Map<String,List<Map<String,Object>>> userAcademicMap = cusUserService.findUserAcademic(userId);
        model.addAttribute("userAcademicMap",userAcademicMap);
        return resultPage("/user/center/academic/u-myAcademic");
    }

    /**
     * 获取用户可以添加的专业列表
     * @param parentIds 排除的专业ID串
     * @return Map<String,Object>
     */
    @RequestMapping("/getSubjectParent")
    @ResponseBody
    public Map<String,Object> getSubjectParent(@RequestParam(value = "parentIds",required = false) String parentIds){
        Map<String,Object> params = new HashMap<>();
        params.put("notIds",parentIds);
        params.put("parentId",0);
        params.put("status",1);
        List<Map<String,Object>> parentSubjectList = subjectService.findSubjectList(params);
        if(ObjectUtils.isNotEmpty(parentSubjectList)){
            return ResultUtil.SUCCESS(parentSubjectList);
        }else{
            return ResultUtil.ERROR("没有可添加的专业");
        }
    }

    /**
     * 专业下所有科目
     * @param parentId
     * @return
     */
    @RequestMapping("/ajax/querySubjectByParentIds")
    @ResponseBody
    public Map<String,Object> querySubjectByParentIds(@RequestParam("parentId") BigDecimal parentId,
                                                      @RequestParam(value = "childIds",required = false) String childIds){
        if(!DataUtil.idIsNotNull(parentId)){
            return ResultUtil.ERROR("请选择相应的专业");
        }
        Map<String,Object> params = new HashMap<>();
        params.put("notIds",childIds);
        params.put("parentId",parentId);
        params.put("status",1);
        List<Map<String,Object>> childSubjectList = subjectService.findSubjectList(params);
        if(ObjectUtils.isNotEmpty(childSubjectList)){
            return ResultUtil.SUCCESS(childSubjectList);
        }else{
            return ResultUtil.ERROR("没有科目可选择");
        }
    }

    /**
     * 添加专业和科目
     * @return
     */
    @RequestMapping("/save/userAddSubject")
    @ResponseBody
    public Map<String,Object> saveUserAddSubject(HttpServletRequest request,
                                                  @RequestParam("subjectIds")String subjectIds){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return cusUserService.updateUserNaturalSubjectIds(userId, subjectIds);
    }

}
